package artist.graphic.cn.UI;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import artist.graphic.cn.R;
import artist.graphic.cn.UI.Fragment.MainFragment;
import artist.graphic.cn.UI.Fragment.UserInMainFragment;
import artist.graphic.cn.UI.Fragment.VideoInMainFragment;
import artist.graphic.cn.Utils.StatusBarUtil;

/**
 * Created by wanghuanlong on 2018/1/3.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //页面控件
    private Button  btn_sms;
    private LinearLayout ll_video, ll_game_center, ll_home, ll_exam, ll_use_male;
    TextView tv_video, tv_game_center, tv_home, tv_exam, tv_use_male;
    TextView[] TVs;

    private int currentIndex = 2;
    private FragmentManager fm;
    private long exitTime = 0;
    private Handler handler;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StatusBarUtil.setTransparentForImageView(this, null);
        setContentView(R.layout.activity_main);

        SharedPreferences pref = getSharedPreferences("first", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("status", false);
        editor.commit();

        fm=getFragmentManager();
        Fragment mainFragment=new MainFragment();
        fm.beginTransaction().replace(R.id.fl_main,mainFragment).commit();
        handler=new Handler();
        InitBlock();

    }

    public void InitBlock() {

        //绑定主页底部按钮
        ll_video = (LinearLayout) findViewById(R.id.ll_video);
        ll_game_center = (LinearLayout) findViewById(R.id.ll_game_center);
        ll_home = (LinearLayout) findViewById(R.id.ll_home);
        ll_exam = (LinearLayout) findViewById(R.id.ll_exam);
        ll_use_male = (LinearLayout) findViewById(R.id.ll_use_male);

        tv_video = (TextView) findViewById(R.id.tv_video);
        tv_game_center = (TextView) findViewById(R.id.tv_game_center);
        tv_home = (TextView) findViewById(R.id.tv_home);
        tv_exam = (TextView) findViewById(R.id.tv_exam);
        tv_use_male = (TextView) findViewById(R.id.tv_use_male);

        TVs = new TextView[5];
        TVs[0] = tv_video;
        TVs[1] = tv_game_center;
        TVs[2] = tv_home;
        TVs[3] = tv_exam;
        TVs[4] = tv_use_male;

        ll_video.setOnClickListener(this);
        ll_game_center.setOnClickListener(this);
        ll_home.setOnClickListener(this);
        ll_exam.setOnClickListener(this);
        ll_use_male.setOnClickListener(this);


    }

    public void quitApp() {
        finish();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.exit(0);
            }
        }, 100);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (false) {
                if (false) {
                } else {
                    if ((System.currentTimeMillis() - exitTime) > 1000) {
                        Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                        exitTime = System.currentTimeMillis();
                    } else {
                        quitApp();
                    }
                }
                return true;
            } else {
                if ((System.currentTimeMillis() - exitTime) > 1000) {
                    Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    quitApp();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_video:

                if(currentIndex!=0){
                    currentIndex = 0;
                    setIndex(currentIndex);
                    Fragment videoInMainFragment=new VideoInMainFragment();
                    fm.beginTransaction().replace(R.id.fl_main,videoInMainFragment).commit();
                }

                break;
            case R.id.ll_game_center:
                currentIndex = 1;
                setIndex(currentIndex);
                Toast.makeText(this,"还未开发出来哟亲，敬请期待！",Toast.LENGTH_SHORT).show();

                break;
            case R.id.ll_home:
                if(currentIndex!=2) {
                    currentIndex = 2;
                    setIndex(currentIndex);
                    Fragment mainFragment = new MainFragment();
                    fm.beginTransaction().replace(R.id.fl_main, mainFragment).commit();
                }
                break;
            case R.id.ll_exam:
                if(currentIndex!=3) {
                    currentIndex = 3;
                    setIndex(currentIndex);
                    startActivity(new Intent(this, HomeActivity.class));
                }
                break;

            case R.id.ll_use_male:
                if(currentIndex!=4) {
                    currentIndex = 4;
                    setIndex(currentIndex);
                    Fragment userFragment = new UserInMainFragment();
                    fm.beginTransaction().replace(R.id.fl_main, userFragment).commit();
                }
                break;

        }
    }

    public void setIndex(int Index) {
        currentIndex = Index;
        for (int i = 0; i < 5; i++) {
            if (i == currentIndex) {
                TVs[i].setTextColor(getResources().getColor(R.color.nav_text_selected_color));
            } else {
                TVs[i].setTextColor(getResources().getColor(R.color.nav_text_unselected_color));
            }
        }

    }



}
